package cn.shiva.redisunit.message.stream;

import io.lettuce.core.RedisBusyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author shiva   2022-08-16 21:51
 */
@Slf4j
public class EventStreamUtils {
    public static void createConsumerGroup(String key, String group, RedisTemplate redisTemplate) {
        try {
            redisTemplate.opsForStream().createGroup(key, group);
        } catch (RedisSystemException e) {
            Throwable cause = e.getRootCause();
            if (cause != null && RedisBusyException.class.equals(cause.getClass())) {
                log.info("STREAM - Redis group already exists, skipping Redis group creation: {}", group);
            } else {
                throw e;
            }
        }
    }
}
